import {IS_FOR_WX} from "../../AppConfig";

if (cc.sys.platform === cc.sys.WECHAT_GAME && IS_FOR_WX) {
  let PlayerData = require("PlayerData");

  let sendLogEvent = function (eventName, data) {
    console.log("sendLogEvent", eventName, data);
    let toSendData = undefined;

    if (typeof data === 'object') {
      toSendData = {};
      let keys = Object.keys(data);
      for (let i = 0; i < keys.length; i++) {
        let key = keys[i];
        toSendData[key] = JSON.stringify(data[key]);
      }
    }
    wx.aldSendEvent(eventName, toSendData);
  };


  let onStageStart = function (stageId, stageName) {
    console.log("onStageStart", stageId, stageName);
    wx.aldStage.onStart({
      stageId: stageId.toString(),     //关卡ID， 必须是1 || 2 || 1.1 || 12.2 格式  该字段必传
      stageName: stageName, //关卡名称，该字段必传
      userId: PlayerData.instance.getUserId(), //用户ID 可选
    });
  };

  let onStageRunning = function (stageId, stageName, eventString, itemName, itemDesc) {
    console.log("onStageRunning", stageId, stageName, eventString, itemName, itemDesc);
    wx.aldStage.onRunning({
      stageId: stageId.toString(),     //关卡ID， 必须是1 || 2 || 1.1 || 12.2 格式  该字段必传
      stageName: stageName, //关卡名称，该字段必传
      userId: PlayerData.instance.getUserId(), //用户ID 可选,
      event: eventString,  //用户触发的操作    该字段必传
      params: {
        itemName,        // 该字段必传
        desc: itemDesc
      }
    });
  };

  let onStageEnd = function (stageId, stageName, complete, descString) {
    console.log("onStageEnd", stageId, stageName, complete, descString);
    wx.aldStage.onEnd({
      stageId: stageId.toString(),    //关卡ID 该字段必传
      stageName: stageName, //关卡名称  该字段必传
      userId: PlayerData.instance.getUserId(), //用户ID 可选
      event: complete ? "complete" : "fail",   //关卡完成  关卡进行中，用户触发的操作    该字段必传
      params: {
        desc: descString   //描述
      }
    });
  };

  module.exports = {
    sendLogEvent,
    onStageStart,
    onStageRunning,
    onStageEnd
  };
}
